// pagesProperty/pages/oa/attendance/leave/index.js
var app = getApp(), fetchApi = require("../../../../../utils/httpPost.js"), 
common_Js = require("../../../../../component/common.js")
import Dialog from '../../../../../component/vant/dialog/dialog';
import Toast from '../../../../../component/vant/toast/toast';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    leaveTypes:[],
    leaveTypeName:'调休',
    leaveTypeCode:'tiaoxiu',
    placeholder:'请假事由',
    submitState:false,
    beginDate:'',
    endDate:'',
    beginTime:'上午',
    endTime:'',
    beginTimeCode:'am',
    endTimeCode:'pm',
    leaveTime:0,
    openTimeType:'am',
    isShowPopupLeave:false,
    isShowPopupTimeView:false,
    isShowPopupTime:false,
    currentDate:new Date().getTime(),
    minDate: new Date(2022, 0, 1).getTime(),
    radioTimeType:'am',
    remark:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.onHttpInit();
    var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    this.data.beginDate = year + '-' + (month >= 10 ? month :'0' + month) 
                      + '-' + (day > 10 ? day :'0' + day);
    this.setData({
      beginDate: this.data.beginDate,
      endDate: this.data.endDate,
    })

  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpInit: function () {
    var that = this
    fetchApi.httpPost('/attendance/leave/view', {
    }, function (res) {
      if (res.data.code == 0) {
        that.setData({
          leaveTypeNames: res.data.data.leaveTypeNames,
          leaveTypes: res.data.data.leaveTypes,
        })
      }
    }, function (res) {
    })
  },
  /**
   * 
   */
  onClickLeaveType(e){
    this.setData({
      isShowPopupLeave: !this.data.isShowPopupLeave
    })
  },
  onClickTimeView(e){
    var type = e.currentTarget.dataset.type;
    if(type == 'end'){
      if(this.data.endDate == ''){
        this.setData({
          endDate: this.data.beginDate,
          endTime:'上午',
          radioTimeType:'am'
        })
      }
    }
    this.data.openTimeType = type;
    console.log('this.data.openTimeType',this.data.openTimeType)
    this.setData({
      openTimeType: type,
      isShowPopupTimeView: !this.data.isShowPopupTimeView
    })
  },
  onRadioChange(e){
    console.log('onRadioChange',e);
    this.data.radioTimeType = e.detail;
  },
  onConfirmLeave(e){
    this.onClickLeaveType();
    console.log('onConfirmLeave',e)
    var index = e.detail.index;
    var name = e.detail.value;

    this.data.leaveTypeCode = this.data.leaveTypes[index].dictValue;
    this.data.leaveTypeName = name;
    this.setData({
      leaveTypeName: name
    })
  },
  onClickTime(e){
    this.setData({
      isShowPopupTime: !this.data.isShowPopupTime
    })
  },
  onConfirmDate(e){
    console.log('onConfirmDate',e)
    var time = new Date(e.detail);
    var month = time.getMonth() + 1;
    var day = time.getDate();
    var date = time.getFullYear() + '-' + (month < 10 ? '0' + month : month )
          + '-' + (day < 10 ? '0' + day :day);
    
    console.log('date',date)

    if(this.data.openTimeType == 'begin'){
      this.data.beginDate = date;
      this.setData({
        beginDate: date
      })
    } else if(this.data.openTimeType == 'end'){
      this.data.endDate = date;
      this.setData({
        endDate: date
      })
    }
    this.onClickTime();
  },
  onConfirmTime(e){
    var timeType = this.data.radioTimeType;
    var openTimeType = this.data.openTimeType;
    console.log('openTimeType',openTimeType)
    console.log('timeType',timeType)
    if(openTimeType == 'begin'){
      if(timeType == 'am'){
        this.data.beginTime = "上午";
        this.data.beginTimeCode = "am";
      }else{
        this.data.beginTime = "下午";
        this.data.beginTimeCode = "pm";
      }
    } else {
      if(timeType == 'am'){
        this.data.endTime = "上午";
        this.data.endTimeCode = "am";
      }else{
        this.data.endTime = "下午";
        this.data.endTimeCode = "pm";
      }
    }
    console.log('beginDate',this.data.beginDate)
    console.log('endDate',this.data.endDate)
    console.log('beginTime',this.data.beginTime)
    console.log('endTime',this.data.endTime)
    console.log('beginTimeCode',this.data.beginTimeCode)
    console.log('endTimeCode',this.data.endTimeCode)
    if(this.data.beginDate!='' && this.data.endDate!='' && this.data.beginDate == this.data.endDate){
      if((this.data.beginTimeCode == 'am' && this.data.endTimeCode == 'am')
      || this.data.beginTimeCode == 'pm' && this.data.endTimeCode == 'pm'){
        this.data.leaveTime = 0.5;
      } else if(this.data.beginTimeCode == 'am' || this.data.endTimeCode == 'pm'){
        this.data.leaveTime = 1;
      }
    }else{
      var days = common_Js.getDiffDay(this.data.beginDate,this.data.endDate);
      console.log('days',days);
      if(this.data.beginTimeCode == 'am' && this.data.endTimeCode == 'pm'){
        days += 1;
      } else if(this.data.beginTimeCode == 'am' && this.data.endTimeCode == 'am'){
        days += 0.5;
      } else if(this.data.beginTimeCode == 'pm' && this.data.endTimeCode == 'pm'){
        days += 0.5;
      } else if(this.data.beginTimeCode == 'pm' && this.data.endTimeCode == 'am'){
        days += 0;
      }
      this.data.leaveTime = days;
    }

    
    this.setData({
      leaveTime: this.data.leaveTime,
      beginTime: this.data.beginTime,
      endTime: this.data.endTime,
      isShowPopupTimeView: !this.data.isShowPopupTimeView
    })
  },
  inputHandle(e){
    console.log('inputHandle',e);
    this.data.remark = e.detail;
  },
  submitOp(){
    var that = this;
    if(this.data.leaveTypeCode == ''){
      Toast("请选择请假类型");
      return;
    }
    if(this.data.beginDate == '' || this.data.beginTimeCode == ''){
      Toast("请选择开始时间");
      return;
    }
    if(this.data.endDate == '' || this.data.endTimeCode == ''){
      Toast("请选择结束时间");
      return;
    }

    if(this.data.remark == ''){
      Toast("请填写请假理由");
      return;
    }

    fetchApi.httpPost('/attendance/leave', {
      leaveBeginDate: that.data.beginDate,
      leaveEndDate: that.data.endDate,
      leaveBeginTime: that.data.beginTimeCode,
      leaveEndTime: that.data.endTimeCode,
      leaveTime:that.data.leaveTime,// 请假时长
      remark: that.data.remark,
    }, function (res) {
      if (res.data.code == 0) {
        Toast.success(res.data.msg);
        setTimeout(function(){
          wx.navigateBack({
            delta: -1,
          })
        },500)
      }
      else{
        Toast.fail(res.data.msg);
      }
    }, function (res) {
    })



  }

})